// pages/user/complain/complain.js
// 获取函数
const handler = require("../../../utils/function.js");
// 获取实例
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        img: app.img,
        complain: '',
        name: '',
        phone: '',
        order_no: '',
        is_upload:false,
        upload_type:0,//上传类型
        upload_img:[],
    },
    //获取投诉内容
    complainInput: function(e) {
        var complain = e.detail.value;
        var testComplain = handler.regTest(complain, '请输入您要投诉的内容');
        this.setData({
            complain
        })
    },
    //获取姓名
    nameinput: function(e) {
        var name = e.detail.value;
        // var testName = handler.regTest(name, '请输入您的姓名');
        this.setData({
            name
        })
    },
    //获取电话号码
    phoneinput: function(e) {
        var phone = e.detail.value;
        // var testPhone = handler.regTest(phone, '请输入您的电话', /^1\d{10}$/);
        this.setData({
            phone
        });
    },
    //确认投诉
    getComplain: function() {
        var that = this;
        var {
            complain,
            name,
            phone
        } = this.data;
        var testComplain = handler.regTest(complain, '请输入您要投诉的内容');
        var testName = handler.regTest(name, '请输入您的姓名');
        var testPhone = handler.regTest(phone, '请输入您的电话', /^1\d{10}$/);
        var allTest = testComplain && testName && testPhone;
        if (allTest) {
            app.request({
                url: api.user.complaints,
                method: 'post',
                data: {
                    order_no: that.data.order_no,
                    content: that.data.complain,
                    name: that.data.name,
                    phone: that.data.phone,
                    img: that.data.upload_img,
                },
                success: function(rs) {
                    if (rs.code == 1) {
                        wx.redirectTo({
                            url: '../../map/orderGoing/orderGoing?order_no=' + that.data.order_no,
                        });
                    }else{
                      wx.showToast({  
                          title: rs.msg,  
                          icon: 'none',  
                          duration: 2000  
                      })  
                    }
                }
            })
        } else {
            var testComplain = handler.regTest(complain, '请输入您要投诉的内容');
            var testName = handler.regTest(name, '请输入您的姓名');
            var testPhone = handler.regTest(phone, '请输入您的电话', /^1\d{10}$/);
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_no: options.order_no
        })
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
    },
    // 打开上传遮罩层
  open_upload:function(){
    this.setData({ is_upload: true })
  },
    // 关闭上传遮罩层
  close_upload:function(){
    this.setData({ is_upload:false})
  },
  // 上传图片
  upload_img:function(){
    let that=this;
    wx.chooseImage({
      count:3,
      success: function(res) {
        that.setData({ upload_type:1})
        if (res.tempFilePaths.length>3)
        wx.showTabBar({
          title:'最多只能上传3张图片',
          icon:'none'
        })
        for (var i = 0; i < res.tempFilePaths.length;i++){
          that.uploadFile(res.tempFilePaths[i]);
        }
      },
    })
  },
  // 上传文件
  uploadFile: function (file){
    /**
     * 1 上传图片
     */
    var tem=[];
    var upload_img = this.data.upload_img;
    let that=this;
    wx.uploadFile({
      url: api.order_comment.multimedia,
      filePath: file,
      name: 'file',
      formData: {
        'access_token': wx.getStorageSync("access_token"),
        '_uniacid': app.siteInfo.uniacid,
        '_acid': app.siteInfo.acid,
      },
      success:function(res){
        var data = JSON.parse(res.data)
        console.log(data.data,'2222222222')
        upload_img.push(data.data)
        that.setData({
          upload_img
        })
      },
    })
  },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    // console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})